/*
 * Copyright 2010 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.anim.updater.filters
{
	import flash.display.DisplayObject;
	import flash.filters.BevelFilter;
	import sweezy.anim.core.IPropertyAnimation;
	import sweezy.anim.core.IUpdater;
	import sweezy.anim.updater.UpdaterProperty;

	public class BevelFilterUpdater extends FilterUpdaterBase implements IUpdater
	{

		public function BevelFilterUpdater()
		{
		}

		public function initialize(element:Object, property:UpdaterProperty, prefix:String, option:Object, target:IPropertyAnimation):void
		{
			var obj:DisplayObject = element as DisplayObject;
			var filter:BevelFilter = getFilter(obj, BevelFilter) as BevelFilter;

			if (option !== null)
			{
				option = prefix === null ? null : option[prefix];
				if (option !== null)
				{
					("angle" in option) && (filter.angle = Number(option.angle));
					("blurX" in option) && (filter.blurX = Number(option.blurX));
					("blurY" in option) && (filter.blurY = Number(option.blurY));
					("distance" in option) && (filter.distance = Number(option.distance));
					("highlightAlpha" in option) && (filter.highlightAlpha = Number(option.highlightAlpha));
					("highlightColor" in option) && (filter.highlightColor = uint(option.highlightColor));
					("knockout" in option) && (filter.knockout = Boolean(option.knockout));
					("quality" in option) && (filter.quality = int(option.quality));
					("shadowAlpha" in option) && (filter.shadowAlpha = Number(option.shadowAlpha));
					("shadowColor" in option) && (filter.shadowColor = uint(option.shadowColor));
					("strength" in option) && (filter.strength = Number(option.strength));
					("type" in option) && (filter.type = String(option.type));
				}
			}

			while (property !== null)
			{
				if (isNaN(property.end))
				{
					property.end = 0;
				}
				if (isNaN(property.start))
				{
					property.name === "angle" ? property.start = filter.angle :
						property.name === "blurX" ? property.start = filter.blurX :
						property.name === "blurY" ? property.start = filter.blurY :
						property.name === "distance" ? property.start = filter.distance :
						property.name === "highlightAlpha" ? property.start = filter.highlightAlpha :
						property.name === "shadowAlpha" ? property.start = filter.shadowAlpha :
						property.start = 0;
				}
				if (property.relative)
				{
					property.end += property.start;
				}

				property = property.next;
			}

			obj.filters = obj.filters;
		}

		public function update(element:Object, property:UpdaterProperty, prefix:String, fraction:Number, option:Object, target:IPropertyAnimation):void
		{
			var obj:DisplayObject = element as DisplayObject;
			var filter:BevelFilter = getFilter(obj, BevelFilter) as BevelFilter;
			var value:Number;

			while (property !== null)
			{
				value = property.start + (property.end - property.start) * fraction;
				value = property.round ? ((value * 2 | 0) - (value | 0)) : value;

				property.name === "angle" ? filter.angle = value :
					property.name === "blurX" ? filter.blurX = value :
					property.name === "blurY" ? filter.blurY = value :
					property.name === "distance" ? filter.distance = value :
					property.name === "highlightAlpha" ? filter.highlightAlpha = value :
					property.name === "shadowAlpha" ? filter.shadowAlpha = value :
					null;

				property = property.next;
			}

			obj.filters = obj.filters;
		}
	}
}
